package com.tupi.project.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.tupi.project.R;
import com.tupi.project.util.StatusBarUtil;
import com.tupi.project.view.refresh.RefreshHandler;
import com.tupi.project.view.refresh.RefreshLayout;

import org.loader.autohideime.HideIMEUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * Created by codeest on 16/8/11.
 * 无MVP的activity基类
 */

public abstract class SimpleActivity extends SupportActivity
{
    
    protected Activity mContext;
    private Unbinder mUnBinder;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);//全屏
        setContentView(getLayout());
        HideIMEUtil.wrap(this);//点击任意位置收缩键盘
        StatusBarUtil.compat(this);//沉浸式状态栏
        
        View view = getWindow().getDecorView().findViewById(android.R.id.content);//获取最外层view
        view.setPadding(0, StatusBarUtil.getStatusBarHeight(this), 0, 0);
        
        mUnBinder = ButterKnife.bind(this);
        mContext = this;
        onViewCreated();
        App.getInstance().addActivity(this);
        initEventAndData();
    }
    
    /**
     * ptr_duration_to_close 回弹延时 默认 200ms，回弹到刷新高度所用时间
     * ptr_duration_to_close_header 头部回弹时间 默认1000ms
     * ptr_keep_header_when_refresh 刷新是保持头部 默认值 true.
     * ptr_pull_to_fresh 下拉刷新 / 释放刷新 默认为释放刷新
     * ptr_ratio_of_header_height_to_refresh 触发刷新时移动的位置比例 默认，1.2f，移动达到头部高度1.2倍时可触发刷新操作。
     * ptr_resistance 阻尼系数 默认: 1.7f，越大，感觉下拉时越吃力。
     */
    protected void setDefaultPullToRefresh(RefreshLayout mPtrFrame, RefreshHandler refreshHandler)
    {
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(refreshHandler);
        // the following are default settings
        mPtrFrame.setResistance(1.7f);
        mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);
        mPtrFrame.setDurationToClose(200);
        mPtrFrame.setDurationToCloseHeader(1000);
        // default is false
        mPtrFrame.setPullToRefresh(false);
        // default is true
        mPtrFrame.setKeepHeaderWhenRefresh(true);
        
    }
    
    
    protected void setToolBar(Toolbar toolbar, String title)
    {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                onBackPressedSupport();
            }
        });
    }
    
    protected void onViewCreated()
    {
        
    }
    
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        App.getInstance().removeActivity(this);
        mUnBinder.unbind();
    }
    
    @Override
    public void finish()
    {
        super.finish();
        //activity出场动画 第二个参数无效
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
    }
    
    @Override
    public void startActivity(Intent intent)
    {
        super.startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
    }
    
    @Override
    public void onBackPressedSupport()
    {
        super.onBackPressedSupport();
        //activity出场动画 第二个参数无效
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
    }
    
    protected abstract int getLayout();
    
    protected abstract void initEventAndData();
}
